package com.alexis.portknocking;

import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;

public class Knock {
	public static boolean doKnock(String host, int port) {
		Socket s;
		try {
			s = new Socket(host, port);
			if (s.isConnected())
				s.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return false;
		} catch (ConnectException e) {
			// We dismiss "connection refused" as knockd operates at link-layer
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
}
